﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using _0912276_ChatClient.DTO;
using System.Collections;

namespace _0912276_ChatClient.BO
{
    public class SearchMember
    {
        public static List<employee> FindMemberWithType(int type)
        {
            using (distributedsystemEntities dse = new distributedsystemEntities())
            {
               int currentyear = DateTime.Today.Year;
                switch (type)
                {
                    case 1:
                        {
                           
                            var employWithSalary = (dse.salaries.GroupBy(r => r.emp_no).Select(
                                g => g.OrderByDescending(x => x.from_date).FirstOrDefault()).Select(p => p.employee));


                            var GoldMember = dse.employees.Where(p => ((currentyear - p.birth_date.Year) < 60));
                            var result = GoldMember.Intersect(GoldMember);
                            int count = result.Count();
                            return GoldMember.ToList();
                        }
                    case 2:
                        {
                            var SilverMember = dse.employees.Where(p => ((currentyear - p.birth_date.Year) < 60)).Take(40);
                            return SilverMember.ToList();
                        }

                    case 3:
                        {
                            var RegularMember = dse.employees.Where(p => (((currentyear - p.birth_date.Year) > 60) && (currentyear - p.birth_date.Year) < 67)).ToList();
                            return RegularMember;
                        }
                    default:
                        return null;
                        
                }
            }
        }
        public static MemberShip FindMemberWithId(int id)
        {
            using (distributedsystemEntities dse = new distributedsystemEntities())
            {
                employee e = (from p in dse.employees
                              where p.emp_no == id
                              select p).First();
                salary slr = e.salaries.OrderByDescending(t => t.to_date).FirstOrDefault();
                Boolean gen;
                if (e.gender == "M")
                    gen = false;
                gen = false;
                MemberShip result = new MemberShip(e.emp_no,e.first_name,e.last_name,
                    "null",e.birth_date,gen,slr.salary1);
                return result;
            }
        }
        public static List<employee> FindMemberWithid(int id)
        {
            using (distributedsystemEntities dse = new distributedsystemEntities())
            {
                try
                {
                    var result = (from p in dse.employees
                                  where p.emp_no == id
                                  select p).ToList();
                    return result;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        public static List<employee> FindMemberWithFirstName(String FirstName)
        {
            using (distributedsystemEntities dse = new distributedsystemEntities())
            {
                try
                {
                    var result = (from p in dse.employees
                                  where p.first_name == FirstName
                                  select p).ToList();
                    return result;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        public static List<employee> FindMemberWithLastName(String LastName)
        {
            using (distributedsystemEntities dse = new distributedsystemEntities())
            {
                try
                {
                    var result = (from p in dse.employees
                                  where p.last_name == LastName
                                  select p).ToList();
                    return result;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        public static List<employee> FindMemberWithFirstNameAndLastName(string firstname, string lastname)
        {
            using (distributedsystemEntities dse = new distributedsystemEntities())
            {
                try
                {
                    var result = (from p in dse.employees
                                  where p.last_name == lastname &&p.first_name==firstname
                                  select p).ToList();
                    return result;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        public static bool CheckYear(employee emp)
        {
            int currentyear = DateTime.Today.Year;
            if ((currentyear - emp.birth_date.Year) < 68)
                return true;
            return false;
        }
        public static IEnumerable<employee> FindMemberWithTypePlus(int type)
        {
            distributedsystemEntities dse = new distributedsystemEntities();
            
                int currentyear = DateTime.Today.Year;
                switch (type)
                {
                    case 1:
                        {
                            #region error
                            //get employee with currrent salary
                            //var employWithSalary = (dse.salaries.GroupBy(r => r.emp_no).Select(
                            //    g => g.OrderByDescending(x => x.from_date).FirstOrDefault()).Select(p => p.employee));
                            //get employee with old less than 60
                            //var GoldMember = dse.employees.Where(p => ((currentyear - p.birth_date.Year) < 60));
                            //Intersect
                            //var result = employWithSalary.Intersect(GoldMember);
                            //count number with 20 percent
                            //int number = employWithSalary.Count() * 20 / 100;
                            //int number = GoldMember.Count();
                            #endregion
                            //count employees and count 20 percent
                            int number = dse.employees.Count() * 20 / 100;
                            List<employee> lstresult = new List<employee>();
                            //check age less than 68
                            var GoldMember = dse.employees.Where(p => ((currentyear - p.birth_date.Year) < 68));
                            //sort result with salary
                            var result = from m in GoldMember
                                         orderby m.salaries.Max(p => p.salary1) descending
                                         select m;
                            //TAKE 20 PERCENT
                            return result.Take(number);
                        }
                    case 2:
                        {
                            //count employees and count 50 percent
                            int number = dse.employees.Count() * 50 / 100;
                            List<employee> lstresult = new List<employee>();
                            //check age less than 68
                            var SilverMember = dse.employees.Where(p => ((currentyear - p.birth_date.Year) < 68));
                            //sort result with salary
                            var result = from m in SilverMember
                                         orderby m.salaries.Max(p => p.salary1) descending
                                         select m;
                            //TAKE 50 PERCENT
                            return result.Take(number);
                        }

                    case 3:
                        {
                            var RegularMember = dse.employees.Where(p => (((currentyear - p.birth_date.Year) > 60) && (currentyear - p.birth_date.Year) < 67)).ToList();
                            return RegularMember;
                        }
                    default:
                        return null;

                }

            }
        
    }
}
